package com.lostreseditores.sabersai.presentation.backingBeans;

import java.util.List;
import java.util.TimeZone;

import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.event.RowEditEvent;

import com.lostreseditores.sabersai.exceptions.ZMessManager;
import com.lostreseditores.sabersai.model.Examen;
import com.lostreseditores.sabersai.model.dto.ExamenDTO;
import com.lostreseditores.sabersai.presentation.businessDelegate.BusinessDelegatorView;
import com.lostreseditores.sabersai.utilities.FacesUtils;


public class ExamenView {
    private InputText txtExagrado;
    private InputText txtExamdescript;
    private InputText txtExamestadon;
    private InputText txtExamnombrev;
    private InputText txtExamordenn;
    private InputText txtBaseidn_Bases;
    private InputText txtExamidn_Examen;
    private InputText txtTipoexamidn_Tipoexamen;
    private InputText txtExamidn;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<ExamenDTO> data;
    private ExamenDTO selectedExamen;

    public ExamenView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            ExamenDTO examenDTO = (ExamenDTO) e.getObject();

            if (txtExagrado == null) {
                txtExagrado = new InputText();
            }

            txtExagrado.setValue(examenDTO.getExagrado());

            if (txtExamdescript == null) {
                txtExamdescript = new InputText();
            }

            txtExamdescript.setValue(examenDTO.getExamdescript());

            if (txtExamestadon == null) {
                txtExamestadon = new InputText();
            }

            txtExamestadon.setValue(examenDTO.getExamestadon());

            if (txtExamnombrev == null) {
                txtExamnombrev = new InputText();
            }

            txtExamnombrev.setValue(examenDTO.getExamnombrev());

            if (txtExamordenn == null) {
                txtExamordenn = new InputText();
            }

            txtExamordenn.setValue(examenDTO.getExamordenn());

            if (txtBaseidn_Bases == null) {
                txtBaseidn_Bases = new InputText();
            }

            txtBaseidn_Bases.setValue(examenDTO.getBaseidn_Bases());

            if (txtExamidn_Examen == null) {
                txtExamidn_Examen = new InputText();
            }

            txtExamidn_Examen.setValue(examenDTO.getExamidn_Examen());

            if (txtTipoexamidn_Tipoexamen == null) {
                txtTipoexamidn_Tipoexamen = new InputText();
            }

            txtTipoexamidn_Tipoexamen.setValue(examenDTO.getTipoexamidn_Tipoexamen());

            if (txtExamidn == null) {
                txtExamidn = new InputText();
            }

            txtExamidn.setValue(examenDTO.getExamidn());

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_clear() {
        if (txtExagrado != null) {
            txtExagrado.setValue(null);
            txtExagrado.setDisabled(true);
        }

        if (txtExamdescript != null) {
            txtExamdescript.setValue(null);
            txtExamdescript.setDisabled(true);
        }

        if (txtExamestadon != null) {
            txtExamestadon.setValue(null);
            txtExamestadon.setDisabled(true);
        }

        if (txtExamnombrev != null) {
            txtExamnombrev.setValue(null);
            txtExamnombrev.setDisabled(true);
        }

        if (txtExamordenn != null) {
            txtExamordenn.setValue(null);
            txtExamordenn.setDisabled(true);
        }

        if (txtBaseidn_Bases != null) {
            txtBaseidn_Bases.setValue(null);
            txtBaseidn_Bases.setDisabled(true);
        }

        if (txtExamidn_Examen != null) {
            txtExamidn_Examen.setValue(null);
            txtExamidn_Examen.setDisabled(true);
        }

        if (txtTipoexamidn_Tipoexamen != null) {
            txtTipoexamidn_Tipoexamen.setValue(null);
            txtTipoexamidn_Tipoexamen.setDisabled(true);
        }

        if (txtExamidn != null) {
            txtExamidn.setValue(null);
            txtExamidn.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        if (btnModify != null) {
            btnModify.setDisabled(true);
        }

        if (btnClear != null) {
            btnClear.setDisabled(false);
        }

        return "";
    }

    public void listener_txtId() {
        Examen entity = null;

        try {
            Integer examidn = new Integer(txtExamidn.getValue().toString());
            entity = BusinessDelegatorView.getExamen(examidn);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (entity == null) {
            txtExagrado.setDisabled(false);
            txtExamdescript.setDisabled(false);
            txtExamestadon.setDisabled(false);
            txtExamnombrev.setDisabled(false);
            txtExamordenn.setDisabled(false);
            txtBaseidn_Bases.setDisabled(false);
            txtExamidn_Examen.setDisabled(false);
            txtTipoexamidn_Tipoexamen.setDisabled(false);
            txtExamidn.setDisabled(false);
            btnSave.setDisabled(false);
            btnDelete.setDisabled(true);
            btnModify.setDisabled(true);
            btnClear.setDisabled(false);
        } else {
            txtExagrado.setValue(entity.getExagrado());
            txtExagrado.setDisabled(false);
            txtExamdescript.setValue(entity.getExamdescript());
            txtExamdescript.setDisabled(false);
            txtExamestadon.setValue(entity.getExamestadon());
            txtExamestadon.setDisabled(false);
            txtExamnombrev.setValue(entity.getExamnombrev());
            txtExamnombrev.setDisabled(false);
            txtExamordenn.setValue(entity.getExamordenn());
            txtExamordenn.setDisabled(false);
            txtBaseidn_Bases.setValue(entity.getBases().getBaseidn());
            txtBaseidn_Bases.setDisabled(false);
            txtExamidn_Examen.setValue(entity.getExamen().getExamidn());
            txtExamidn_Examen.setDisabled(false);
            txtTipoexamidn_Tipoexamen.setValue(entity.getTipoexamen()
                                                     .getTipoexamidn());
            txtTipoexamidn_Tipoexamen.setDisabled(false);
            txtExamidn.setValue(entity.getExamidn());
            txtExamidn.setDisabled(true);
            btnSave.setDisabled(true);
            btnDelete.setDisabled(false);
            btnModify.setDisabled(false);
            btnClear.setDisabled(false);
        }
    }

    public String action_save() {
        try {
            BusinessDelegatorView.saveExamen(FacesUtils.checkInteger(
                    txtExagrado), FacesUtils.checkString(txtExamdescript),
                FacesUtils.checkInteger(txtExamestadon),
                FacesUtils.checkInteger(txtExamidn),
                FacesUtils.checkString(txtExamnombrev),
                FacesUtils.checkInteger(txtExamordenn),
                FacesUtils.checkInteger(txtBaseidn_Bases),
                FacesUtils.checkInteger(txtExamidn_Examen),
                FacesUtils.checkInteger(txtTipoexamidn_Tipoexamen));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete() {
        try {
            BusinessDelegatorView.deleteExamen(FacesUtils.checkInteger(
                    txtExamidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            BusinessDelegatorView.updateExamen(FacesUtils.checkInteger(
                    txtExagrado), FacesUtils.checkString(txtExamdescript),
                FacesUtils.checkInteger(txtExamestadon),
                FacesUtils.checkInteger(txtExamidn),
                FacesUtils.checkString(txtExamnombrev),
                FacesUtils.checkInteger(txtExamordenn),
                FacesUtils.checkInteger(txtBaseidn_Bases),
                FacesUtils.checkInteger(txtExamidn_Examen),
                FacesUtils.checkInteger(txtTipoexamidn_Tipoexamen));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
            action_clear();
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String actionDeleteDataTableEditable() {
        try {
            if (txtExamidn == null) {
                txtExamidn = new InputText();
            }

            txtExamidn.setValue(selectedExamen.getExamidn());

            BusinessDelegatorView.deleteExamen(FacesUtils.checkInteger(
                    txtExamidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data.remove(selectedExamen);
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(Integer exagrado, String examdescript,
        Integer examestadon, Integer examidn, String examnombrev,
        Integer examordenn, Integer baseidn_Bases, Integer examidn_Examen,
        Integer tipoexamidn_Tipoexamen) throws Exception {
        try {
            BusinessDelegatorView.updateExamen(exagrado, examdescript,
                examestadon, examidn, examnombrev, examordenn, baseidn_Bases,
                examidn_Examen, tipoexamidn_Tipoexamen);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("ExamenView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtExagrado() {
        return txtExagrado;
    }

    public void setTxtExagrado(InputText txtExagrado) {
        this.txtExagrado = txtExagrado;
    }

    public InputText getTxtExamdescript() {
        return txtExamdescript;
    }

    public void setTxtExamdescript(InputText txtExamdescript) {
        this.txtExamdescript = txtExamdescript;
    }

    public InputText getTxtExamestadon() {
        return txtExamestadon;
    }

    public void setTxtExamestadon(InputText txtExamestadon) {
        this.txtExamestadon = txtExamestadon;
    }

    public InputText getTxtExamnombrev() {
        return txtExamnombrev;
    }

    public void setTxtExamnombrev(InputText txtExamnombrev) {
        this.txtExamnombrev = txtExamnombrev;
    }

    public InputText getTxtExamordenn() {
        return txtExamordenn;
    }

    public void setTxtExamordenn(InputText txtExamordenn) {
        this.txtExamordenn = txtExamordenn;
    }

    public InputText getTxtBaseidn_Bases() {
        return txtBaseidn_Bases;
    }

    public void setTxtBaseidn_Bases(InputText txtBaseidn_Bases) {
        this.txtBaseidn_Bases = txtBaseidn_Bases;
    }

    public InputText getTxtExamidn_Examen() {
        return txtExamidn_Examen;
    }

    public void setTxtExamidn_Examen(InputText txtExamidn_Examen) {
        this.txtExamidn_Examen = txtExamidn_Examen;
    }

    public InputText getTxtTipoexamidn_Tipoexamen() {
        return txtTipoexamidn_Tipoexamen;
    }

    public void setTxtTipoexamidn_Tipoexamen(
        InputText txtTipoexamidn_Tipoexamen) {
        this.txtTipoexamidn_Tipoexamen = txtTipoexamidn_Tipoexamen;
    }

    public InputText getTxtExamidn() {
        return txtExamidn;
    }

    public void setTxtExamidn(InputText txtExamidn) {
        this.txtExamidn = txtExamidn;
    }

    public List<ExamenDTO> getData() {
        try {
            if (data == null) {
                data = BusinessDelegatorView.getDataExamen();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<ExamenDTO> examenDTO) {
        this.data = examenDTO;
    }

    public ExamenDTO getSelectedExamen() {
        return selectedExamen;
    }

    public void setSelectedExamen(ExamenDTO examen) {
        this.selectedExamen = examen;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }
}
